package com.tucanstubs.android_wheresmystuff.activities;

import java.util.List;
import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.database.ItemHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.tucanstubs.android_wheresmystuff.items.Item;

/**
 * This class is the activity for the item information screen.
 *  It displays the item information and allows the user to perform
 *  actions on the item. If the item belongs to the user they can search for similar
 *  items, if the item does not belong to the user they can claim the item.
 * @author Team 37
 * @version 2.0 4/9/2013
 *
 */
public class ItemInfoActivity extends Activity {
		
	private Button claim, simSearch;
	private Item item;
	private String userName;
	
	/**
	 * Creates the activity.Initializes all GUI Components and retrieves information form the bundle.
	 *  The activity receives a bundle with keys Tags.ITEM and Tags.CLASS. 
	 * Tags.ITEM maps to the id of the item whose information we are displaying. Tags.CLASS maps to
	 * the name of the previous activity, this is used to determine which buttons to display.
	 * If the user is looking at their own items they should be able to search for similar items
	 * if they are looking at items returned from a search they should be able to claim an item.
	 * @param SavedInstantceState not used
	 */
	public void onCreate(Bundle SavedInstanceState){
		
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.item_info_screen);
		//makes references to the buttons
		claim = (Button) findViewById(R.id.Claim_Button);
		simSearch = (Button) findViewById(R.id.Similar_Item_Search_Button);
		Bundle bundle = getIntent().getExtras();
		String prevClass = bundle.getString(Tags.CLASS);
		userName = bundle.getString(Tags.USERNAME);
		
		//makes a reference to the selected item
		item=ItemHelper.getItem(this, bundle.getInt(Tags.ITEM));
		Log.i(Tags.ITEMINFOACTIVITY, "prev is: "+prevClass);
		//determines whether to deactivate the "claim" or "similar search" button
		// if the user is looking at their own item then the "claim" button
		// is disabled. If the user is looking at a item from a searched list
		// then the "similar search" button is disabled.
		if(prevClass.equals("SearchItemActivity")){
			Log.i(Tags.ITEMINFOACTIVITY, "simSearch invisible");
			simSearch.setVisibility(View.GONE);
			
		}else if(prevClass.equals("HomeScreenActivity")){
			Log.i(Tags.ITEMINFOACTIVITY, "claim invisible");
			claim.setVisibility(View.GONE);
		}
		
		//Creates the adapter for the list to display the item's info
		List<String> info = item.toList();
		ListView itemsInfo = (ListView) findViewById(R.id.Item_Info_List);
		ListAdapter adapter = new ArrayAdapter<String>(this,
				  android.R.layout.simple_list_item_1, android.R.id.text1, info);
		itemsInfo.setAdapter(adapter);
	}

	/**
	 * Called when a button is clicked, determines which button was pressed and performs the appropriate
	 * action. The item is either claimed, or control is transfered to SearchItemActivity to
	 * search for similar items.
	 * @param view The button which was pressed
	 */
	public void onClick(View view){
		Intent nextIntent;
		switch(view.getId()){
		//find which button was clicked
		case R.id.Claim_Button:
			//set item to matched up
			ItemHelper.setMatched(getApplicationContext(), true, item.getId());
			Toast toast= Toast.makeText(getApplicationContext(), "Claimed", Toast.LENGTH_SHORT);  
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			finish();
			break;
		case R.id.Similar_Item_Search_Button:
			//transfer control to the search activity, put proper info in the bundle
			nextIntent = new Intent(this,SearchItemActivity.class);
			Bundle bundle = new Bundle();
			bundle.putInt(Tags.ITEM,item.getId());
			bundle.putString(Tags.USERNAME, userName);
			nextIntent.putExtras(bundle);
			startActivity(nextIntent);
			break;
		}
	}

}

